package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseAttrInfo;
import com.atguigu.gmall.model.product.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("/admin/product")
@RestController
@Slf4j
public class BaseAttrInfoController {

    @Autowired
    BaseAttrInfoService baseAttrInfoService;

    @Autowired
    BaseAttrValueService baseAttrValueService;

    /*
    * 查询分类下的平台属性
    * */
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result<List<BaseAttrInfo>> attrInfoList(
            @PathVariable("category3Id") Long category1Id,
            @PathVariable("category3Id") Long category2Id,
            @PathVariable("category3Id") Long category3Id,
            HttpSession session){

        List<BaseAttrInfo> baseAttrInfos = baseAttrInfoService.attrInfoList(category1Id,category2Id,category3Id);

        return Result.ok(baseAttrInfos);
    }

    /*
    * 保存平台属性名和值
    * saveAttrInfo( BaseAttrInfo baseAttrInfo): 表单提交的k=v&k=v映射到BaseAttrInfo、
      saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo): 前端提交的son封装映射到BaseAttrInfo
    *
    * */
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        log.info("前端提交的数据：" + baseAttrInfo);

//        baseAttrInfoService.saveInfoAndValue(baseAttrInfo);
        baseAttrInfoService.saveOrUpdateInfoAndValue(baseAttrInfo);
        return Result.ok();
    }

    /*
     * 根据平台属性ID获取平台属性对象数据
     * */
    @GetMapping("/getAttrValueList/{attrId}")
    public Result<List<BaseAttrValue>> getAttrValueList(@PathVariable("attrId") Long attrId){
        QueryWrapper<BaseAttrValue> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_id",attrId);
        List<BaseAttrValue> list = baseAttrValueService.list(wrapper);

        return Result.ok(list);
    }
}
